var WxParse = require('../../common/wxParse/wxParse.js');
var App = getApp();
Page({
  data: {
    detail: {},
    cartCount:0,
    reviews:{},
    specificationArray:[],
    indicatorDots: true,
    autoplay: false,
    interval: 3000,
    duration: 1000,
    tabs: ["商家详情",  "商品评价"],
    activeIndex: 0,
    num:1, // 商品数量
    minusStatuses: '',
    flag:0,
    firstIndex: -1,
    attrValueList: [],
    LENGTH: 5,
    CLS_ON: 'on',
    CLS_OFF: 'off'
  },
  onLoad (param) {
    wx.showNavigationBarLoading()
    wx.setNavigationBarTitle({
      title: param.merchantName // 页面标题为路由参数
    })
    this.setData({shopId: param.merchantId}) // 商家id
    this.shopDetail()
  },
  shopDetail () { // 商家详情
    App.HttpService.shopDetail({shopId: 36}).then(res => {
      wx.hideNavigationBarLoading()
      let data = res.data
      if (data.code == 200) {
        this.setData({detail: data.data})
        // WxParse.wxParse('article', 'html', data.data.introduction, this, 5)
      }
    })
  },
  initData () {
    const revies = {
      items: [],
      total: '',
      params: {
        page : 1,
        pageCount: 10
      }
    }
    this.setData({ revies: revies })
  },
  shopRevies () { // 获取商家评价
    let  {revies, shopId } = this.data
    let params = revies.params
    params.shopId = 36
    wx.showNavigationBarLoading()
    App.HttpService.shopRevies(params).then(res => {
      wx.hideNavigationBarLoading()
      let data = res.data.data
      data.revieList.forEach(item => {
        item.scores = this.itemClasses(item.revieSorce)
      })
      revies.items = [...revies.items, ...data.revieList]
      if (data.revieList.length > 0) {
        params.page = params.page + 1
        revies.total = data.counts
      }            
      this.setData({revies})
      console.log(revies)
    })
  },
  tabClick (e) {
    this.setData({
      activeIndex: e.currentTarget.id
    })
    if (this.data.activeIndex == 1) {
      this.initData()
      this.shopRevies()
    }
  },
  onReachBottom () {
    let revies = this.data.revies
    if (revies.items.length === revies.total) {
      return
    }
    this.shopRevies()
  },
  itemClasses(SCORE) {
    let { LENGTH, CLS_ON, CLS_OFF } = this.data
    let result = []
    let score = Math.floor(SCORE * 2) / 2
    let integer = Math.floor(score)
    for (let i = 0; i < integer; i++) {
      result.push(CLS_ON)
    }
    while (result.length < LENGTH) {
      result.push(CLS_OFF)
    }
    return result
  }
})